#include "oelocalaccount.h"
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>





OELocalAccount::OELocalAccount(QObject *parent)
    : QObject(parent),tableName_("audit_account")
{

}

int OELocalAccount::open(const QString &_dbFileName)
{
    if (db_.databaseName() == _dbFileName) {
        if (!db_.isOpen() && !db_.open()) {
            const QString log = QString("%1 %2(%3) %4: %5")
                    .arg(tr("open")).arg(tr("database"))
                    .arg(_dbFileName).arg(tr("failed"))
                    .arg(db_.lastError().text());
            qWarning() << log;
            return 110;
        }
    }
    else {
        if (db_.isOpen()) {
            db_.close();
        }
        if (QSqlDatabase::contains(_dbFileName)) {
            db_ = QSqlDatabase::database(_dbFileName);
        }
        else {
            db_ = QSqlDatabase::addDatabase("QSQLITE",_dbFileName);
            db_.setDatabaseName(_dbFileName);
        }
    }
    return 0;
}

int OELocalAccount::close()
{
    logout();
    db_.close();
    return 0;
}

int OELocalAccount::setTableName(const QString &_tableName)
{
    tableName_ = _tableName;
    return 0;
}

int OELocalAccount::login(const QString &_user,
                          const QString &_pswd,
                          QString &out_name)
{
    if (!db_.isOpen()) {
        open("auditapi.db");
    }
    const QString& sql = QString("SELECT name FROM %1 "\
                                 "WHERE user = '%2' AND "\
                                 "pswd = '%3';")
            .arg(tableName_).arg(_user).arg(_pswd);
    QSqlQuery query(db_);

    if (!query.exec(sql)) {
        qDebug() << "login failed, sql exec failed.";
        return -1;
    }

    while(query.next()) {
        out_name = query.value(0).toString();
        if (query.next())
            qCritical() << "login appears with same account.";
    }
    if (out_name.isEmpty())
        return -1;

    info_.user = _user;
    info_.pswd = _pswd;
    info_.name = out_name;

    const QString& log = QString("%1(%2) %3")
            .arg(tr("user")).arg(info_.name)
            .arg(tr("login"));
    qInfo() << log.toUtf8().data();

    return 0;
}

int OELocalAccount::logout(void) {
    /// [1] clean login resource


    // please write code to here


    /// ![1] clean login resource end

    /// [2] out log to debug
    if (!info_.name.isEmpty()) {
        const QString& log = QString("%1(%2) %3")
                .arg(tr("user")).arg(info_.name)
                .arg(tr("log out"));
        qInfo() << log.toUtf8().data();
    }


    /// [3] clean user info
    info_.name="";
    info_.user="";
    info_.pswd="";

    return 0;
}

int OELocalAccount::checkPswd(const QString &_pswd)
{
    if (_pswd.isEmpty())
        return 87;

    if (info_.pswd.isEmpty())
        return -1;

    if (info_.pswd != _pswd)
        return 1;

    return 0;
}

bool OELocalAccount::isLogin(void)
{
    if (info_.pswd.isEmpty()
        || info_.user.isEmpty())
        return false;
    return true;
}
